import 'package:flutter/material.dart';
import 'package:flutter_app_demo/podo/forecast_list_model.dart';
import 'package:flutter_app_demo/ui_demo/ui_weather_forecast/top/CustomAppBar.dart';
import 'package:flutter_app_demo/ui_demo/ui_weather_forecast/generic/slide_drawer.dart';
import 'package:flutter_app_demo/ui_demo/ui_weather_forecast/weather_list/forecast_list.dart';
import 'package:flutter_app_demo/ui_demo/ui_weather_forecast/weather_list/main_weather.dart';
import 'package:flutter_app_demo/ui_demo/ui_weather_forecast/right/week_menu.dart';

class WeatherPage extends MaterialPageRoute<WeatherPage> {
    WeatherPage()
        : super(builder: (BuildContext context) {
        return WeatherDemo();
    });
}

class WeatherDemo extends StatefulWidget {
    @override
    _WeatherDemoState createState() => _WeatherDemoState();
}

class _WeatherDemoState extends State<WeatherDemo> with TickerProviderStateMixin {
    
    MenuController _menuController; //右侧菜单动画控制器
    SlidingListController _slidingListController; //列表动画控制器
    
    String tempString = '星期一, 6月10日';
    
    @override
    void initState() {
        super.initState();
        _menuController = MenuController(
            vsync: this,
            openDuration: Duration(
                milliseconds: 250
            )
        )
            ..addListener(() {
                setState(() {
                
                });
            });
        
        _slidingListController = SlidingListController(
            
            itemCount: data.items.length,
            vsync: this,
        
        )
            ..open()
        ;
    }
    
    @override
    void dispose() {
        _menuController.dispose();
        _slidingListController.dispose();
        super.dispose();
    }
    
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            body:

//            Builder(
//                builder: (BuildContext ctx) {//测试代码，为了显示SnackBar
//                    return
            
            Stack(
                children: <Widget>[
                    
                    WeatherList(
                        slidingListController: _slidingListController,
                    ),
                    
                    Positioned( // 注意，此处在stack内自定义appbar的方式！
                        
                        top: 10,
                        left: 0,
                        right: 0,
                        child: MyAppBar(
                            onMenuClick: _menuController.open,
                            weekString: tempString,
                        ),
                    )
                    ,
                    
                    SlidingAWidget(
                        menuController: _menuController,
                        drawer: WeekMenu(
                            onDaySelect: (String title) {
                                
                                ///日期选择之后需要干的事情：
                                
                                ///1.替换标题
                                setState(() {
                                    tempString = title.replaceAll("\n", ", ");
                                });
                                
                                ///2.关闭菜单
                                _menuController.close();
                                
                                ///3.列表动画先关闭再打开，注意此处的UI序列操作逻辑，所以需要将close和open定义为异步执行
                                _slidingListController.close().then(
                                        (_) {
                                        _slidingListController.open();
                                    }
                                );

//                                Scaffold.of(ctx)
//                                    ..removeCurrentSnackBar()
//                                    ..showSnackBar(SnackBar(content: Text("$title")));
                            },
                        ),
                    ),
                ],
            ),
//                },
//            )
        );
    }
}




